package com.itheima.service.db.impl;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.SmsHomeNewProduct;
import com.itheima.mapper.SmsHomeNewProductMapper;
import com.itheima.service.db.SmsHomeNewProductService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeNewProductServiceImpl implements SmsHomeNewProductService {


    @Autowired
    SmsHomeNewProductMapper smsHomeNewProductMapper;
    @Override
    public void save(SmsHomeNewProduct newProduct) {
        smsHomeNewProductMapper.insert(newProduct);
    }

    @Override
    public void deleteNewProduct(List<Long> ids) {
        smsHomeNewProductMapper.deleteBatchIds(ids);
    }

    @Override
    public PageBeanVo findByPage(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        Page<SmsHomeNewProduct> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<SmsHomeNewProduct> qw = new LambdaQueryWrapper<>();

        qw.like(StrUtil.isNotEmpty(productName), SmsHomeNewProduct::getProductName, productName);
        qw.eq(recommendStatus != null,SmsHomeNewProduct::getRecommendStatus, recommendStatus);
        qw.orderByAsc(SmsHomeNewProduct::getSort);

        Page<SmsHomeNewProduct> smsHomeNewProductPage = smsHomeNewProductMapper.selectPage(page, qw);

        return new PageBeanVo(pageNum,pageSize,smsHomeNewProductPage.getTotal(),smsHomeNewProductPage.getRecords());
    }

    @Override
    public void updateNewProduct(Long id, Integer recommendStatus) {
        SmsHomeNewProduct newProduct = new SmsHomeNewProduct();
        newProduct.setId(id);
        newProduct.setRecommendStatus(recommendStatus);
        smsHomeNewProductMapper.updateById(newProduct);
    }

    @Override
    public void updatesortNewProduct(Long id, Integer sort) {
        SmsHomeNewProduct newProduct = new SmsHomeNewProduct();
        newProduct.setId(id);
        newProduct.setSort(sort);
        smsHomeNewProductMapper.updateById(newProduct);
    }
}
